package com.ls.tree.binarysorttree;

public class BinarySortTreeDemo {
    public static void main(String[] args) {
        final BinarySortTree tree = new BinarySortTree();

        int[] arr = {7, 3, 10, 12, 5, 1, 9, 2};

        for (int i : arr) {
            tree.add(new Node(i));
        }

        tree.preOrder();
        System.out.println();
        tree.delNode(3);
        tree.preOrder();
        Node search = tree.search(5);
        System.out.println();
        System.out.println(search.left);
        System.out.println(search.right);

    }
}


class BinarySortTree {


    Node root;

    public void delNode(int value) {
        if (root == null) {
            return;
        } else {
            Node target = search(value);
            if (target == null) {
                return;
            }
            //如果树只有一个节点
            if (root.left == null && root.left == null) {
                root = null;
                return;
            }
            Node targetParent = searchParent(value);

            if (target.left == null && target.right == null) {//如果删除目标是叶子节点
                if (targetParent.left != null && targetParent.left.value == value) {
                    targetParent.left = null;
                } else if (targetParent.right != null && targetParent.right.value == value) {
                    targetParent.right = null;
                }
            } else if (target.left != null && target.right != null) {//如果删除目标有两个子树节点
                int minVal = delRightTreeMin(target.right);
                target.value = minVal;
            } else {//如果删除目标有一个子树节点
                if (target.left != null) {
                    if (targetParent == null) {//只有两个节点的情况下
                        root = target.left;
                    } else {
                        if (targetParent.left.value == value) {
                            targetParent.left = target.left;
                        } else {
                            targetParent.right = target.left;
                        }
                    }

                } else {
                    if (targetParent == null) {
                        root = target.right;
                    } else {
                        if (targetParent.left.value == value) {
                            targetParent.left = target.right;
                        } else {
                            targetParent.right = target.right;
                        }
                    }

                }
            }


        }
    }

    /**
     * 删除以node为根节点的二叉排序树的最小值
     *
     * @param node 当做二叉排序树的根节点
     * @return 返回以node为根节点的二叉排序树的最小节点的值
     */
    public int delRightTreeMin(Node node) {
        Node target = node;
        while (target.left != null) {
            target = target.left;
        }
        delNode(target.value);
        return target.value;
    }


    public Node searchParent(int value) {
        if (root == null) {
            return null;
        } else {
            return root.searchParent(value);
        }
    }

    public Node search(int value) {
        if (root == null) {
            return null;
        } else {
            return root.search(value);
        }

    }

    public void add(Node node) {
        if (root == null) {
            root = node;
        } else {
            root.add(node);
        }
    }

    public void infixOrder() {
        if (root == null) {
            return;
        }
        root.infixOrder();
    }

    public void preOrder() {
        if (root == null) {
            return;
        }
        root.preOrder();
    }
}


class Node {
    int value;
    Node left;
    Node right;

    public Node(int value) {
        this.value = value;
    }


    /**
     * 查找要删除节点的父节点
     *
     * @param value
     * @return
     */
    public Node searchParent(int value) {
        if ((this.left != null && this.left.value == value) ||
                (this.right != null && this.right.value == value)) {
            return this;
        } else {
            if (value < this.value && this.left != null) {
                return this.left.searchParent(value);
            } else if (value >= this.value && this.right != null) {
                return this.right.searchParent(value);
            } else {
                return null;
            }
        }
    }


    /**
     * 查找要删除的节点
     *
     * @param value
     * @return
     */
    public Node search(int value) {
        if (value == this.value) {
            return this;
        } else if (value < this.value) {
            if (this.left == null) {
                return null;
            }
            return this.left.search(value);
        } else {
            if (this.right == null) {
                return null;
            }
            return this.right.search(value);
        }
    }


    /**
     * 递归的方式添加节点
     *
     * @param node
     */
    public void add(Node node) {
        if (node == null) {
            return;
        }
        if (node.value < this.value) {
            if (this.left == null) {
                this.left = node;
            } else {

                this.left.add(node);
            }
        } else {
            if (this.right == null) {
                this.right = node;
            } else {
                this.right.add(node);
            }
        }
    }

    public void preOrder() {

        System.out.println(this);
        if (this.left != null) {
            this.left.preOrder();
        }
        if (this.right != null) {
            this.right.preOrder();
        }
    }

    public void infixOrder() {

        if (this.left != null) {
            this.left.infixOrder();
        }
        System.out.println(this);
        if (this.right != null) {
            this.right.infixOrder();
        }
    }

    @Override
    public String toString() {
        return "Node{" +
                "value=" + value +
                '}';
    }
}
